package com.bkitmobile.kesu.android.service;

import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;

import com.bkitmobile.kesu.android.ClassInfo.PlaceInfo;
import com.bkitmobile.kesu.android.ClassInfo.UserConfig;
import com.bkitmobile.kesu.android.GUI.PlaceList;
import com.bkitmobile.kesu.android.GUI.R;
import com.bkitmobile.kesu.android.GUI.main;

public class Alert {
	private NotificationManager mNotificationManager;
	private int CHAT_NOTFICATION_ID;
	private int PLACE_NOTFICATION_ID;
	private Context context;
	private Notification notifyDetails;
	private long[] vibrate = {100,100,200,300};
	public Alert(Context context)
	{
		this.context = context;
		mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		notifyDetails = new Notification(R.drawable.more,context.getString(R.string.alert_notify),System.currentTimeMillis());
		//mNotificationManager.cancelAll();
	}

	public void doAlertPlace(List<PlaceInfo> placesInfo,int typeAlert,int ID)
	{
		PLACE_NOTFICATION_ID = ID;
		if (typeAlert == UserConfig.RING)
		{
			ringPlace(placesInfo);
		} else if (typeAlert == UserConfig.VIBRATE)
		{
			vibratePlace(placesInfo);
		} else if (typeAlert == UserConfig.RING_AND_VIBRATE)
		{
			ringAndVibrate(placesInfo);
		}
	}
	public void doAlertChat(String username,String message,int typeAlert,int ID)
	{
		CHAT_NOTFICATION_ID = ID;
		if (typeAlert == UserConfig.RING)
		{
			ringChat(username,message);
		} else if (typeAlert == UserConfig.VIBRATE)
		{
			vibrateChat(username,message);
		} else if (typeAlert == UserConfig.RING_AND_VIBRATE)
		{
			ringAndVibrateChat(username,message);
		}
	}
	private void ringChat(String username,String message)
	{
		notifyDetails.defaults = Notification.DEFAULT_SOUND;
		alertChat(username,message);
	}
	private void vibrateChat(String username,String message)
	{
		notifyDetails.vibrate = vibrate;
		alertChat(username,message);
	}
	private void ringAndVibrateChat(String username,String message)
	{
		notifyDetails.vibrate = vibrate;
		notifyDetails.defaults =Notification.DEFAULT_ALL;
		alertChat(username,message);
	}
	private void alertChat(String username,String message)
	{
		 Intent notifyIntent = new Intent(context, main.class);

	        PendingIntent intent =
	          PendingIntent.getActivity(context, 0,
	           notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
	        	CharSequence contentTitle = username;
	            CharSequence contentText = message;
	        	notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
	        mNotificationManager.notify(CHAT_NOTFICATION_ID, notifyDetails);
	}
	private void vibratePlace(List<PlaceInfo> placesInfo)
	{
		notifyDetails.vibrate = vibrate;
		alertPlace(placesInfo);
	}
	
	private void alertPlace(List<PlaceInfo> placesInfo)
	{
		//CharSequence contentTitle = "Androidpeople.com Simple Notification";
        //CharSequence contentText = "Get back to Application on clicking me";
		//cancel(PLACE_NOTFICATION_ID);
        Intent notifyIntent = new Intent(context, PlaceList.class);
        notifyIntent.putExtra("search", false);
        notifyIntent.putExtra("size", placesInfo.size());
        int i = 0;
        for (PlaceInfo tmp:placesInfo)
        {
        	notifyIntent.putExtra("place"+i, tmp);
        	i++;
        }
        PendingIntent intent =
          PendingIntent.getActivity(context, 0,
           notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        CharSequence contentTitle = context.getString(R.string.alert_title);
        CharSequence contentText = context.getString(R.string.alert_content);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
        
        mNotificationManager.notify(PLACE_NOTFICATION_ID, notifyDetails);
	}
	private void ringPlace(List<PlaceInfo> placesInfo)
	{
		notifyDetails.defaults = Notification.DEFAULT_SOUND;
		alertPlace(placesInfo);
	}

	private void ringAndVibrate(List<PlaceInfo> placesInfo)
	{
		notifyDetails.vibrate = vibrate;
		notifyDetails.defaults = Notification.DEFAULT_ALL;
		alertPlace(placesInfo);
	}
	public void cancel(int ID)
	{
		mNotificationManager.cancel(ID);
	}
}
